<?php

namespace App\Jobs;

use App\Models\RcUser;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class WxMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected  $wx_token;
    protected $template;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($template,$wx_token,$client)
    {
        $this->template = $template;
        $this->wx_token = $wx_token;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //调取微信接口发送模板消息
        $wx_url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$this->wx_token;
        $client = new Client(['','timeout'=> 5.0,]);
            $params = [
                'touser'=>$this->template['open_id'],
                'page'=>$this->template['page'],
                'template_id'=>$this->template['template_id'],
                'data'=>$this->template['te_data']
            ];
            $response2 = $client->post($wx_url,['json'=>$params]);
            $content = json_decode($response2->getBody()->getContents());
            Log::info('模板消息',[$content]);
    }
}
